package com.learn.matrix;

import java.io.FileOutputStream;

/**
 * 矩阵的之字打印
 */
public class MatrixZigPrint {
    public void zigPrint(int[][] arr) {
        //A的行号，列号
        int Ar = 0;
        int Ac = 0;
        int Br = 0;
        int Bc = 0;
        int endR = arr.length;
        int endC = arr[0].length;
        boolean fromUp = false;
        while (Ar != endR) {
            print(arr, Ar, Ac, Br, Bc, fromUp);
            Ar = Ac == endC ? Ar + 1 : Ar;
            Ac = Ac == endC ? Ac : Ac + 1;

            Br = Br == endR ? Br : Br + 1;
            Bc = Br == endR ? Bc + 1 : Bc;

            fromUp = !fromUp;

        }
    }


    public void print(int[][] arr, int Ar, int Ac, int Br, int Bc, boolean fromUp) {
        if (fromUp) {
            while (Ar != Br + 1) {
                System.out.println(arr[Ar++][Ac--] + " ");
            }
        } else {
            while (Br != Ar - 1) {
                System.out.println(arr[Br--][Bc++] + " ");
            }
        }
    }
}

